alter PROCEDURE sp_InsertKhuyenMai
	@Ma_KhuyenMai nchar (10),
	@Ma_MonAn nchar (10),
	@PhanTramGiam int,
	@NgayBatDau datetime,
	@NgayKetThuc datetime
AS
BEGIN TRAN
	DECLARE  @NBD datetime
	DECLARE @NKT datetime
	DECLARE @cur CURSOR
	DECLARE @FLAG int
	SET @FLAG=0
	SET @cur= CURSOR FOR (SELECT NgayBatDau,NgayKetThuc FROM KhuyenMai WHERE Ma_MonAn=@Ma_MonAn)
	OPEN @CUR
	FETCH NEXT FROM @cur INTO @NBD,@NKT
	WHILE @@fetch_status = 0
	BEGIN
		IF((@NgayBatDau between @NBD and @NKT)OR (@NgayKetThuc between @NBD and @NKT) OR ((@NBD between @NgayBatDau and @NgayKetThuc) and (@NKT between @NgayBatDau and @NgayKetThuc)) )
		BEGIN
				SET @FLAG=1
		END
		
		FETCH NEXT FROM @cur INTO @NBD,@NKT
	END
	
	waitfor delay '0:0:05'

	IF(@FLAG=0)
	BEGIN
		INSERT INTO KhuyenMai VALUES(@Ma_KhuyenMai,@Ma_MonAn,@PhanTramGiam,@NgayBatDau,@NgayKetThuc)
	END
COMMIT TRAN 

EXEC sp_InsertKhuyenMai '015','004',10,'2013-07-03 00:00:00.000','2013-07-09 00:00:00.000'
